﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExternalConnections.CWMComm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExternalConnections.Res;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using WMCommLib;


namespace DnProcessSimulateCommands.ExternalConnections;

internal class CWMComm : IWMCommConnectNotify
{
  private IWMCommConnect m_pCommItf;
  private IWMCommAdvise m_pCommAdviceItf;
  private IWMCommRecv m_pCommRecv;
  private IWMCommSend m_pCommSend;
  private string m_hostName;
  private ushort m_port;
  private bool m_bCommConnected;
  private readonly byte[] c_keyClient = new byte[176 /*0xB0*/]
  {
    (byte) 164,
    (byte) 163,
    (byte) 0,
    (byte) 0,
    (byte) 160 /*0xA0*/,
    (byte) 0,
    (byte) 0,
    (byte) 0,
    (byte) 215,
    (byte) 119,
    (byte) 86,
    (byte) 72,
    (byte) 62,
    (byte) 172,
    (byte) 125,
    (byte) 141,
    (byte) 241,
    (byte) 212,
    (byte) 50,
    (byte) 169,
    (byte) 250,
    (byte) 128 /*0x80*/,
    (byte) 225,
    (byte) 202,
    (byte) 10,
    (byte) 86,
    (byte) 46,
    (byte) 75,
    (byte) 48 /*0x30*/,
    (byte) 80 /*0x50*/,
    (byte) 59,
    (byte) 58,
    (byte) 30,
    (byte) 159,
    (byte) 168,
    (byte) 120,
    (byte) 26,
    (byte) 37,
    (byte) 2,
    (byte) 217,
    (byte) 182,
    (byte) 135,
    (byte) 249,
    (byte) 171,
    (byte) 253,
    (byte) 128 /*0x80*/,
    (byte) 237,
    (byte) 243,
    (byte) 149,
    (byte) 210,
    (byte) 63 /*0x3F*/,
    (byte) 135,
    (byte) 203,
    (byte) 9,
    (byte) 215,
    (byte) 68,
    (byte) 200,
    (byte) 47,
    (byte) 102,
    (byte) 239,
    (byte) 249,
    (byte) 119,
    (byte) 16 /*0x10*/,
    (byte) 248,
    (byte) 4,
    (byte) 10,
    (byte) 102,
    (byte) 179,
    (byte) 150,
    (byte) 9,
    (byte) 4,
    (byte) 55,
    (byte) 189,
    (byte) 238,
    (byte) 131,
    (byte) 248,
    (byte) 195,
    (byte) 72,
    (byte) 10,
    (byte) 178,
    (byte) 156,
    (byte) 187,
    (byte) 18,
    (byte) 64 /*0x40*/,
    (byte) 165,
    (byte) 116,
    (byte) 240 /*0xF0*/,
    (byte) 182,
    (byte) 188,
    (byte) 24,
    (byte) 29,
    (byte) 64 /*0x40*/,
    (byte) 134,
    (byte) 184,
    (byte) 193,
    (byte) 85,
    (byte) 84,
    (byte) 68,
    (byte) 45,
    (byte) 53,
    (byte) 15,
    (byte) 26,
    (byte) 211,
    (byte) 11,
    (byte) 73,
    (byte) 179,
    (byte) 213,
    (byte) 199,
    (byte) 67,
    (byte) 82,
    (byte) 89,
    (byte) 168,
    (byte) 122,
    (byte) 139,
    (byte) 101,
    (byte) 7,
    (byte) 188,
    (byte) 250,
    (byte) 249,
    (byte) 178,
    (byte) 61,
    (byte) 200,
    (byte) 60,
    (byte) 158,
    (byte) 153,
    (byte) 46,
    (byte) 52,
    (byte) 79,
    (byte) 173,
    (byte) 122,
    (byte) 119,
    (byte) 244,
    (byte) 176 /*0xB0*/,
    (byte) 183,
    (byte) 69,
    (byte) 63 /*0x3F*/,
    (byte) 196,
    (byte) 193,
    (byte) 100,
    (byte) 232,
    (byte) 65,
    (byte) 2,
    (byte) 220,
    (byte) 95,
    (byte) 244,
    (byte) 138,
    (byte) 164,
    (byte) 127 /*0x7F*/,
    (byte) 160 /*0xA0*/,
    (byte) 191,
    (byte) 73,
    (byte) 47,
    (byte) 239,
    (byte) 222,
    (byte) 92,
    (byte) 186,
    (byte) 207,
    (byte) 71,
    (byte) 48 /*0x30*/,
    (byte) 230,
    (byte) 250,
    (byte) 178,
    (byte) 173,
    (byte) 188,
    (byte) 186,
    (byte) 5,
    (byte) 111,
    (byte) 145,
    (byte) 174,
    (byte) 128 /*0x80*/,
    (byte) 169,
    (byte) 117,
    (byte) 23,
    (byte) 78,
    (byte) 159,
    (byte) 205
  };
  private const uint m_uNotifyStates = 22;
  public readonly List<string> m_COMElements = new List<string>();

  public event EventHandler<EventArgs> ConnectionStateChanged;

  public event EventHandler<EventArgs> ComElementListChanged;

  public event EventHandler<EventArgs> ConnectionFailed;

  public CWMComm()
  {
    try
    {
      this.m_pCommItf = (IWMCommConnect) Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("B8CE9F89-95A2-4B68-97A6-3566AFBFC3F9")));
      if (this.m_pCommItf == null)
        return;
      // ISSUE: reference to a compiler-generated method
      this.m_pCommSend = this.m_pCommItf.CreateSendBuffer();
      // ISSUE: reference to a compiler-generated method
      this.m_pCommRecv = this.m_pCommItf.CreateRecvBuffer();
      this.m_pCommAdviceItf = (IWMCommAdvise) this.m_pCommItf;
      // ISSUE: reference to a compiler-generated method
      this.m_pCommAdviceItf.AdviseConnectItf((IWMCommConnectNotify) this, 22U, eWMCommNotify.eWMCommNotify_SingleThreadAsync);
    }
    catch (Exception ex)
    {
      if (TxMessageBox.ShowModal(CUiECExternalConnectionsResourceTable.ERR_MSG_COM_NOT_REGISTRED, CUiECExternalConnectionsResourceTable.ERR_MSG_COM_NOT_REGISTRED_CAPTION, MessageBoxButtons.YesNo, MessageBoxIcon.Hand) != DialogResult.Yes)
        return;
      Process.Start("https://www.winmod.de/ps200/");
    }
  }

  public void GetElementList()
  {
    if (!this.IsConnected() || this.m_pCommSend == null)
      return;
    // ISSUE: reference to a compiler-generated method
    this.m_pCommSend.SendTelegram(0U, 9U);
  }

  public void OnCommNotify(uint nCommStates, uint nDeltaStates)
  {
    if (this.m_pCommItf == null)
      return;
    if (((eWMCommState) nDeltaStates).HasFlag((Enum) eWMCommState.eWMCommStateConnect))
    {
      if (((eWMCommState) nCommStates).HasFlag((Enum) eWMCommState.eWMCommStateConnect))
      {
        this.m_bCommConnected = true;
        this.GetElementList();
      }
      else
      {
        this.m_bCommConnected = false;
        this.m_COMElements.Clear();
      }
      this.OnConnectionStateChanged(EventArgs.Empty);
    }
    if (!((eWMCommState) nCommStates).HasFlag((Enum) eWMCommState.eWMCommStateRecv))
      return;
    try
    {
      uint num;
      // ISSUE: reference to a compiler-generated method
      while ((num = this.m_pCommRecv.RecvTelegram(out uint _)) != 0U)
      {
        if (num != 1073741833U /*0x40000009*/)
        {
          if (num == 1073741840U /*0x40000010*/ || num == 1073741843U /*0x40000013*/)
            ;
        }
        else
        {
          this.ReadComElementList();
          this.OnComElementListChanged(EventArgs.Empty);
        }
      }
    }
    catch (Exception ex)
    {
      this.OnConnectionFailed(EventArgs.Empty);
    }
  }

  public void CommConnect(string hostName, ushort port, uint timeOut)
  {
    try
    {
      if (this.IsConnected())
        this.CommDisconnect();
      if (this.IsConnected() || this.m_pCommItf == null)
        return;
      this.m_hostName = hostName;
      this.m_port = port;
      // ISSUE: reference to a compiler-generated method
      this.m_pCommItf.Connect(ref this.c_keyClient[0], Convert.ToUInt32(this.c_keyClient.Length), hostName, port, timeOut);
    }
    catch (Exception ex)
    {
      this.OnConnectionFailed(EventArgs.Empty);
    }
  }

  public bool IsConnected() => this.m_bCommConnected;

  public void CommDisconnect()
  {
    try
    {
      this.m_bCommConnected = false;
      if (this.m_pCommItf != null)
      {
        try
        {
          // ISSUE: reference to a compiler-generated method
          this.m_pCommItf.Disconnect();
        }
        catch (Exception ex)
        {
        }
      }
      if (this.m_pCommSend == null)
        return;
      // ISSUE: reference to a compiler-generated method
      this.m_pCommSend.ResetTelegram();
    }
    catch (Exception ex)
    {
    }
  }

  private void OnConnectionStateChanged(EventArgs e)
  {
    EventHandler<EventArgs> connectionStateChanged = this.ConnectionStateChanged;
    if (connectionStateChanged == null)
      return;
    connectionStateChanged((object) this, e);
  }

  private void OnComElementListChanged(EventArgs e)
  {
    EventHandler<EventArgs> elementListChanged = this.ComElementListChanged;
    if (elementListChanged == null)
      return;
    elementListChanged((object) this, e);
  }

  private void OnConnectionFailed(EventArgs e)
  {
    EventHandler<EventArgs> connectionFailed = this.ConnectionFailed;
    if (connectionFailed == null)
      return;
    connectionFailed((object) this, e);
  }

  private void ReadComElementList()
  {
    this.m_COMElements.Clear();
    // ISSUE: reference to a compiler-generated method
    uint num = this.m_pCommRecv.ReadUInt32();
    for (uint index = 0; index < num; ++index)
    {
      // ISSUE: reference to a compiler-generated method
      this.m_COMElements.Add(this.m_pCommRecv.ReadBSTR());
    }
  }
}
